/**
 * Advanced Software Engineering - assignment part 1 (monica Farrow)
 * Parcel class
 * 
 * Some of the methods in this class have been based on lecture notes provided by
 * Monica Farrow as a part of Software Engineering courses at Heriot-Watt University.
 */

package depot;


/**
 * Advanced Software Engineering - F21AS.
 * Parcel class with parcel details.
 * @author Ionut Iordache, Nikky Afunugo, Dave Bond
 */
public class Parcel {

	private String parcelID;
	private int[] dimensions;
	private double weight;
	private int days;


	/** 
	 * constructor for Parcel
	 * Creates an Parcel object with values specified in the parameters
	 * @param id		the ID of the parcel
	 * @param dim		parcel dimensions
	 * @param wgt		parcel weight
	 * @param d			no. of days the parcel has been in the depot
	 * @throws IllegalArgumentException
	 */
	public Parcel(String id, int[] dim, double wgt, int d) {

		// check for correct ID length
		if(id.length() != 7) {
			throw new IllegalArgumentException("ParcelID must contain 7 characters");
		}

		// check first character of ID is valid
		if(! id.substring(0, 1).matches("[ESX]*")) {
			throw new IllegalArgumentException("ParcelID must start with E, S or X");
		}

		// check last 6 characters of ID are digits
		if(! id.substring(1, 6).matches("[0-9]*")) {
			throw new IllegalArgumentException("Last 6 characters of ParcelID must be digits");
		}

		// check each dimension is > 0
		for (int i = 0; i < dim.length; i++) {
			if(dim[i] < 1){
				throw new IllegalArgumentException("dim["+i+"] must be > 0");
			}
		}

		// check volume of parcel is <= 226,535 cubic centimeters (roughly 2 square feet)
		if(dim[0]*dim[1]*dim[2] > 226535) {
			throw new IllegalArgumentException("Volume must be less than 226,535 cubic centimeters");
		}

		// check weight of parcel is > 0.1 and < 10 kilos
		if((wgt <= 0) || (wgt > 10)) {
			throw new IllegalArgumentException("Parcel weight must be between 0.01 and 10.00 kilos");
		}

		// check no. of days in depot is > 0 and < 28
		if((d < 1) || (d > 28)) {
			throw new IllegalArgumentException("Number of days must be between 1 and 28");
		}

		parcelID = id;
		dimensions = dim;
		weight = wgt;
		days = d;
	}


	/** 
	 * returns the ID of the parcel
	 * @return the parcel ID
	 */
	public String getParcelID(){
		return parcelID;
	}


	/** 
	 * returns the dimensions of the parcel
	 * @return the parcel dimensions
	 */
	public int[] getDimensions() {
		return dimensions;
	}

	/**
	 *  returns the individual parcel dimensions as a string
	 * @return  individual parcel dimensions
	 */
	public String getDimensionsAsString() {
		String allDims = "";
		for (int i=0;i<dimensions.length;i++) {
			allDims += dimensions[i] + "*";
		}
		allDims = allDims.trim();
		// remove last "*" from each dimension
		allDims = allDims.substring(0, allDims.length() - 1);
		return allDims; 
	}


	/**
	 *  sets the parcel dimensions to the value provided in the parameter
	 * @param dim  parcel dimensions
	 */
	public void setDimensions(int[] dim) {
		dimensions = dim;
	}


	/** 
	 * returns the volume of the parcel
	 * @return the parcel volume
	 */
	public int getVolume() {
		int volume = 1;

		for( int index = 0; index < dimensions.length; index++ )
		volume *= dimensions[index];
		
		return volume;
	}

	/** 
	 * returns the weight of the parcel
	 * @return the parcel weight
	 */
	public double getWeight() {
		return weight;
	}


	/** 
	 * sets the parcel weight to the value provided in the parameter
	 * @param wgt	 the parcel weight
	 */
	public void setWeight(double wgt) {
		weight = wgt;
	}


	/** 
	 * returns no. of days the parcel has been in the depot
	 * @return the no. of days
	 */
	public int getDays() {
		return days;
	}


	/** 
	 * sets no. of days the parcel has been in the depot 
	 * to the value provided in the parameter
	 * @param d	 the parcel weight
	 */
	public void setDays(int d) {
		days = d;
	}

	/** 
	 * overrides equals methods higher up the hierarchy
	 * first check that other object being compared IS a Parcel
	 * if so, cast to an Parcel
	 * @return true if equal, false otherwise
	 */
	public boolean equals(Object other){
		if (!(other instanceof Parcel) )
			return false;
		Parcel otherPcl = (Parcel) other;
		return (this.parcelID.equals(otherPcl.parcelID));
	}

	
	/**
	 *  toString method for Parcel class
	 * @return  parcel's ID, dimensions, weight and days in depot
	 */
	public String toString() {
		return "ID: " + this.getParcelID() + ",  Dimensions: " + this.getDimensionsAsString() + ", Weight: " + this.getWeight() +",  Days: " + this.getDays();
	}


}

